package com.example.warehouse.contrallor;

import com.example.warehouse.dto.AssignAuthDto;
import com.example.warehouse.entity.Result;
import com.example.warehouse.entity.Role;
import com.example.warehouse.page.Page1;
import com.example.warehouse.service.RoleService;
import com.example.warehouse.utils.CurrentUser;
import com.example.warehouse.utils.TokenUtils;
import com.example.warehouse.utils.WarehouseConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class contrallorRole {

    @Autowired
    private RoleService roleService;


    @RequestMapping("/role-list")
    public Result queryAllRole(){

        List<Role> roleList = roleService.getAllRole();

        return Result.ok(roleList);
    }

    @RequestMapping("/role-page-list")
    public Result roleListPage(Page1 page1,Role role){
        page1 = roleService.QueryRolePage(page1, role);
        return Result.ok(page1);
    }

    @DeleteMapping("/role-delete/{roleId}")
    public Result deleteById(@PathVariable Integer roleId){
        roleService.deleteAuthByRoleId(roleId);
        boolean b = roleService.DeleteById(roleId);
        if(!b){
            return Result.err(Result.CODE_ERR_BUSINESS,"删除角色失败");
        }
        return Result.ok(Result.CODE_OK);
    }

    @PutMapping("/role-state-update")
    public Result updateRoleState(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setUpdateBy(currentUser.getUserId());
        role.setUpdateTime(new Date());
        boolean b = roleService.updateRoleState(role);
        if(!b){
            return Result.err(Result.CODE_ERR_BUSINESS,"角色状态更改失败");
        }
        return Result.ok(Result.CODE_OK);
    }
    @Autowired
    private TokenUtils tokenUtils;
    @PutMapping("/role-update")
    public Result updateRoleById(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setUpdateTime(new Date());
        role.setUpdateBy(currentUser.getUserId());
        roleService.updateRoleById(role);
        return Result.ok(role);
    }

    @PostMapping("/role-add")
    public Result addRole(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setCreateTime(new Date());
        role.setCreateBy(currentUser.getUserId());
        role.setRoleState("1");
        roleService.addRole(role);
        return Result.ok(role);
    }


    @GetMapping("/role-auth")
    public Result RoleAuth(Integer roleId){
        List<Integer> authList = roleService.FindAuthByroleId(roleId);
        return Result.ok(authList);
    }

    @PutMapping("/auth-grant")
    public Result updateRoleAuth(@RequestBody AssignAuthDto assignAuthDto){
        roleService.deleteAuthByRoleId(assignAuthDto.getRoleId());
        roleService.AddAuthByRoled(assignAuthDto);
        return Result.ok();
    }


}
